package com.whp.yky.yky.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JwtUtil {

    private static String secret = "WHP@hahahahahahaha";

    /*
    生成token
    * */
    public static String getToken(Map<String,String> user) {

        Calendar nowDate = Calendar.getInstance();
        nowDate.add(Calendar.SECOND,60);

        JWTCreator.Builder builder = JWT.create();
        user.forEach((k,v)->{
            builder.withClaim(k,v);
        });

        String token =  builder.withExpiresAt(nowDate.getTime()).sign(Algorithm.HMAC256("secret"));



        return token;

    }

    /*
    验证token
    * */
    public static DecodedJWT verify(String token){

            DecodedJWT verify = JWT.require(Algorithm.HMAC256("secret")).build().verify(token);
            return verify;
    }

}
